<?php

// namespace App\Http

// use App\Http\Requests;

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;
use DB;
use Symfony\Component\HttpFoundation\Request;

use Auth;

class PhotoController extends Controller
{
    //博客首页
    public function index(){
         $boke = DB::table('boke')->orderBY('id','asc')->paginate(3);
         //dd($atrs);
        return view('art.index', ['boke' => $boke]);

        //return view("art.index");

    }
    //添加页面
    public function add(){
        //return ("添加文章");
        //$users = DB::table('post')->get();
        //dd($users);
        return view("art.add");
    }
    //添加页面处理··1·
    public function doAdd(){
        $title=$_POST['title'];
        $content=$_POST['content'];

        $sql= DB::table('boke')->insert(['title' => $title, 'content' => $content,]);
        if($sql){
            // alert("添加成功");
            //echo "1";
            //header("refresh:2;url=http://localhost/add");
            return redirect("index");
        }
        return("添加失败");

    }
    //阅读
    public function read($id){

        //echo $id;
        $art= DB::table('boke')->where('id','=',$id)->first();
        if($art){
            //return(1);
            return view('art.read',['art'=>$art]);
        }
        return("阅读失败");
    }
    //修改
    public function update($id){
        $art= DB::table('boke')->where('id','=',$id)->first();
        if($art){
            //return(1);
            return view("art.update",['art'=>$art]);
        }
    }
    ////修改处理
    public function doUpdate(){
        $id=$_POST['id'];
        $res= DB::table('boke')->where('id',$id)
            ->update(['title'=>$_POST['title'],
            'content'=>$_POST['content']]);

        if($res){
            //return(1);
            //return ("修改成功");
            return redirect("index");
        }
    }
    //删除
    public function del(){
        $id=$_POST['id'];
        $res = DB::table('boke')->where('id',$id)->delete();
        if($res){
            //echo 1;
//            return ("删除成功");
            return redirect("index");
        }
    }

    public function user()
    {
        //用户信息
        //dd(Auth::user());
        //登录状态
        dd(Auth::check());
    }
    public function logout()
    {
        //退出登录
        Auth::Logout();
    }
    public function postLogin(Request $request)
    {

        $res = Auth::attempt(['email' => $request->email, 'password' => $request->password]);
        //var_dump($res);die;
        if($res){
            return redirect("index");
        }else{
            return back();
        }
    }


}